<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PublicController extends BaseController
{
    /**
     * 图片上传
     */
    public function uploadImg(){
        $this->uid = \session('uid');
//        base64数据
        $base64Data = \request()->input('img', '');
        if( $base64Data && strpos($base64Data, ';base64,') !== false ){
            $data = explode(';base64,', $base64Data);
            if( strpos($data[0], 'image/') !== false ){
                $type = explode('image/', $data[0])[1];
                $name = md5(mt_rand(10000, 99999).time().$this->uid);
                $path = public_path().DIRECTORY_SEPARATOR.'uploads'.DIRECTORY_SEPARATOR.'orderImg'.DIRECTORY_SEPARATOR.$this->uid.DIRECTORY_SEPARATOR;
//                echo $path;exit;
                if( !file_exists($path) ){
                    mkdir($path, 0777, true);
                }

                file_put_contents($path.$name.'.'.$type, base64_decode($data[1]));
//                return returnJson(0, '', ['url'=>'public'.DIRECTORY_SEPARATOR.'orderImg'.DIRECTORY_SEPARATOR.$this->uid.DIRECTORY_SEPARATOR.$name.'.'.$type]);
                return returnJson(0, '', ['url'=>DIRECTORY_SEPARATOR.'uploads'.DIRECTORY_SEPARATOR.'orderImg'.DIRECTORY_SEPARATOR.$this->uid.DIRECTORY_SEPARATOR.$name.'.'.$type]);
            }
        }
        return returnJson(1, '图片信息有误');
    }

    /**
     * 店铺图片识别
     */
    public function storeImgDistinguish(){
        $this->uid = \session('uid');
//        base64数据
        $base64Data = \request()->input('img', '');
        if( $base64Data && strpos($base64Data, ';base64,') !== false ){
            $data = explode(';base64,', $base64Data);
            if( strpos($data[0], 'image/') !== false ){
                $data = imgRecognition2($data[1]);
                $storeData = ['title'=>'', 'phone'=>'', 'address'=>''];
                $str = '';
                foreach ($data as $key => $value) {
                    if( $value['words'] == '>' ) {
                        unset($data[$key]);
                    }
                }
                $data = array_values($data);
                foreach ($data as $key => $value) {
                    if( $value['words'] == '门店名称' ){
                        $storeData['title'] = $data[$key+1]['words'];
                        if( $data[$key+2]['words'] != '经营品类' && $data[$key+2]['words'] != '门店ID' && $data[$key+2]['words'] != '门店D' ) $storeData['title'] .= $data[$key+2]['words'];
                    }elseif( $value['words'] != '门店名称' && strpos($value['words'], '门店名称') !== false ){
                        $storeData['title'] = mb_substr($value['words'], 4);
                        if( $data[$key+1]['words'] != '经营品类' && $data[$key+1]['words'] != '门店ID' && $data[$key+1]['words'] != '门店D' ) $storeData['title'] .= $data[$key+1]['words'];
                    }
                    if( $value['words'] == '订单电话' || $value['words'] == '餐厅电话' ){
                        $storeData['phone'] = trim($data[$key+1]['words'], '>');
                    }

                    if( $value['words'] == '门店地址' || $value['words'] == '餐厅地址' ){
                        $storeData['address'] = $data[$key+1]['words'];
                        if( $data[$key+2] != '营业时间' && $data[$key+1]['words'] != '门店分类'  ) $storeData['address'] .= $data[$key+2]['words'];
                    }elseif( ($value['words'] != '门店地址' && strpos($value['words'], '门店地址') !== false) || ($value['words'] != '餐厅地址' && strpos($value['words'], '餐厅地址') !== false) ){
                        $storeData['address'] = mb_substr($value['words'], 4);
                        if( $data[$key+1]['words'] != '营业时间' && $data[$key+1]['words'] != '门店分类' ) $storeData['address'] .= $data[$key+1]['words'];
                    }
                    $str .= $value['words'].' ';

                }
                return returnJson(0, '', ['data'=>$storeData, 'str'=>$str]);
            }
        }
        return returnJson(1, '图片信息有误');
    }
}